package com.wanxi.dangdang.mapper;

import com.wanxi.dangdang.model.dto.AdminDelAllDto;
import com.wanxi.dangdang.model.dto.AdminFindPageDto;
import com.wanxi.dangdang.model.pojo.Admin;
import com.wanxi.dangdang.model.pojo.Role;
import com.wanxi.dangdang.model.vo.AdminFindPageVo;
import com.wanxi.dangdang.model.vo.AdminRoleVo;
import com.wanxi.dangdang.model.vo.AdminVo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: 吕永杰
 * @Description:
 * @Date: Created in 2021/8/30
 * @Version: 1.0
 */
@Repository
public interface AdminMapper {

    //检验是否纯在这个账号
    @Select("select account from admin where account=#{account}")
    String checkAccount(String account);

    @Insert("insert into admin (account,password,name,super_manager_id,create_time,update_time)" +
            " value (#{account},#{password},#{name},#{superManagerId},NOW(),NOW())")
    void add(Admin admin);

    @Select("select id,name from admin where id=#{id}")
    Admin findById(Long id);

    @Select("<script> select DISTINCT a.id,a.name,a.enable_status from admin a LEFT JOIN admin_role_relation ar on a.id=ar.admin_id" +
            " where a.delete_status=0 " +
            "<if test='enableStatus!=null'> and a.enable_status = #{enableStatus}</if>" +
            "<if test='name!=null'> and a.name like CONCAT('%',#{name},'%')</if>" +
            "<if test='rId!=null'> and ar.role_id =#{rId}</if>" +
            " </script>")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "name",column = "name"),
            @Result(property = "enableStatus",column = "enable_status"),
            @Result(property = "roles",
                    column = "id",
                    javaType = List.class,
                    many = @Many(select = "com.wanxi.dangdang.mapper.AdminMapper.findRolesByUserId"))
    })
    List<AdminFindPageVo> findPage(AdminFindPageDto adminFindPageDto);

    //连表查询
    @Select("select r.* from admin_role_relation ar left join role r on r.id=ar.role_id where ar.admin_id=#{id} ")
    List<Role> findRolesByUserId(Long id);

    @Update("update admin set name=#{name},update_time=NOW() where id=#{id}")
    void updateById(Admin admin);

    @Update("update admin set delete_status=1,update_time=NOW() where id = #{id}")
    void deleteById(Long id);

    @Update("update admin set enable_status=ABS(deleteStatus-1),update_time=NOW() where id = #{id}")
    void enableStatusId(Long id);

    /**
     * 批量插入多条记录
     * @param "adminRoleVo.roles" 存储roleId的数组
     * @return 成功插入的条数
     */
    @Insert({
            "<script>",
            "insert into admin_role_relation (admin_id, role_id,create_time,update_time) values ",
            // collection 和 value对应   item代表循环中List的这一项IpMap
            "<foreach collection='roles' item='roleId' index='index' separator=','>",
            "(#{id}, #{roleId},NOW(),NOW())",
            "</foreach>",
            "</script>"
    })
    void updateRolesById(AdminRoleVo adminRoleVo);

    @Select("select account,id,password from admin where account=#{account}")
    AdminVo loadUserByUsername(String account);

    @Update("update admin set password=#{password},update_time=NOW() where id = #{id}")
    void rePassword(Admin admin);

    //删除admin role中间表的权限
    @Delete("delete from admin_role_relation where admin_id=#{id}")
    void deleteAdminRoleById(Long id);

}
